package 力扣;

import java.util.ArrayList;
import java.util.List;


/**
 * @author yyq
 * @create 2022-04-15 15:33
 */
public class leetcode98 {
    public static void main(String[] args) {
        TreeNode root =new TreeNode(5);
        root.left=new TreeNode(4);
        root.right=new TreeNode(6);
        root.right.left=new TreeNode(3);
        root.right.right=new TreeNode(7);
        leetcode98 l=new leetcode98();
        l.isValidBST(root);
    }
    public boolean isValidBST(TreeNode root) {
        if(root==null) return false;
        int[] a = new int[1];
        int[] start = new int[1];
        start[0] = -1;
        boolean flag = inOrder(root, a,start);
        return flag;
    }

    private boolean inOrder(TreeNode root, int[] a,int[] start) {
        boolean flag = true;
        if(root.left!=null){
            flag = inOrder(root.left,a,start);
            if(flag==false) return false;
        }

        if(start[0]==-1) {
            a[0] = root.val;
            start[0] = 1;
        }
        else {

            if(root.val<=a[0]) return false;
            a[0] = root.val;
        }

        if(root.right!=null){
            flag = inOrder(root.right,a,start);
            if(flag==false) return false;
        }

        return flag;
    }


}
